package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.exce.FindException;
import com.qf.mapper.UserMapper;
import com.qf.pojo.TbUser;
import com.qf.result.Result;
import com.qf.service.UserService;
import com.qf.status.StatusCode;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/31 17:07
 */
@Service
@Transactional//开启事务注解
public class UserServiceImpl implements UserService {
    private UserMapper userMapper;
    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public PageInfo<TbUser> findPageUser(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbUser> list=userMapper.findPageUser(pageNum,pageSize);
        Assert.isTrue(list!=null, ()->{
            throw new FindException("你想要的数据不存在",StatusCode.DATA_NOT_EXIST);
        });
        PageInfo<TbUser> pageInfo= new PageInfo<>();
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public void updateUser(TbUser tbUser) {
        userMapper.updateUser(tbUser);
    }

    @Override
    public TbUser findById(long id) {
        //断言,当前的判断条件一定符号内部的表达式的要求,否则就会执行后面的操作
//        断言条件和if else相似,只是条件肯定是存在成功,不然直接运行后面的异常
        Assert.isTrue(id>0,()->{
            throw new FindException("id不符合要求", StatusCode.ID_NOT_ALLOWED);
        });
        TbUser user = userMapper.findById(id);
        Assert.isTrue(user!=null,()->{
            throw new FindException("数据不存在",StatusCode.DATA_NOT_EXIST);
        });
        return user;
    }

    @Override
    public void deleteById(long id) {
        Assert.isTrue(id>0, ()->{
            throw new FindException("请选择删除的用户", StatusCode.ID_NOT_ALLOWED);
        });
        userMapper.deleteById(id);

    }
}
